
import PlatformUtil from "./PlatformUtil";
import NumberView from "./NumberView";
import httpUtils from "./XmlHttpTool";
import allVariableJs from "./allVariable";
cc.Class({

    extends: cc.Component,

    properties: {

        startGameBtn:cc.Button,
        numberCompontent:NumberView,
        energyCompontent:NumberView,
        bgMusicSound:{
            default: null,
            type: cc.AudioClip
        },
        topView:cc.Node,
        logonode:cc.Node,
        wordListView:cc.Node,
        wordShowView:cc.Node,
        friendListView:cc.Node,
        bageAllScrollView:cc.Node,
        inviteBtn:cc.Button,
        rankBtn:cc.Button,
        moreBtn:cc.Button,
        shopBtn:cc.Button,
        moreContentView:cc.Node,
        showHubProjess:cc.Node,
        SvprogressPrefab: cc.Prefab,
        userEnergy:cc.Integer,
        addenergyBtn:cc.Button,
        insufficientAllView:cc.Node,
        insufficientClose:cc.Button,
        playNowBtn:cc.Button,
        exchangeEnergyView:cc.Node,
        isshareSuccess:cc.Boolean
       

    },

    enterGameViewClick:function(){
        sdkbox.PluginAdMob.hide("home")
        if(this.userEnergy>0){
            var changebig2 = cc.scaleTo(0.2,1.1);

            var finish = cc.callFunc(function(){
        
              var changesmail2 = cc.scaleTo(0.1,1.0);
              this.startGameBtn.node.runAction(changesmail2)
        
           }, this);
        
           var seq = cc.sequence(changebig2,finish)
        
            this.startGameBtn.node.runAction(seq)
    
            cc.director.loadScene("GameSence",function(){
    
                var mainNode= cc.find("Canvas/backgroundtop")
    
                mainNode.getComponent("backgrould").onAgainSence();
    
    
           });
             
        }else{

            console.log("体力不足不能开始游戏")
            if(cc.isValid(this.showHubProjess)){
                this.showHubProjess.destroy();
              }
              this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
              this.node.addChild(this.showHubProjess);
              this.showHubProjess.getComponent('SVProgressView').init("Insufficient energy unable to participate")
        
               let finish = cc.callFunc(() => { 
              
                this.showHubProjess.destroy();
            
              }, this);
            let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(0.5), cc.fadeOut(1), finish);
            this.showHubProjess.runAction(action);

        }
    },

    onLoad () {

      
        if(allVariableJs.isMusicPlaying){


        }else{
           
           this.current = cc.audioEngine.play(this.bgMusicSound, true, 1);
           allVariableJs.isMusicPlaying=true
        }


        var changebig2 = cc.scaleTo(0.2,1.2);

        var finish = cc.callFunc(function(){

        var changesmail2 = cc.scaleTo(0.1,1.0);
        this.logonode.runAction(changesmail2)

    }, this);

     var seq = cc.sequence(changebig2,finish)

     this.logonode.runAction(seq)


     var self = this;
     self.kHomeBanner = "home";
     self.kGameOverAd = "gameover";
     self.kRewardedAd = "rewarded";

     var initSDK = function() {
        if ("undefined" == typeof(sdkbox)) {
            console.log("sdkbox is not exist")
            return
        }

        if ("undefined" != typeof(sdkbox.PluginAdMob)) {
            var plugin = sdkbox.PluginAdMob
            plugin.setListener({
                adViewDidReceiveAd: function(name) {
                    console.log('adViewDidReceiveAd name='+name)
                    if (name == self.kHomeBanner) {
                        plugin.show(name);
                    }
                },
                adViewDidFailToReceiveAdWithError: function(name, msg) {
                    console.log('adViewDidFailToReceiveAdWithError name='+name+' msg='+msg);
                },
                adViewWillPresentScreen: function(name) {
                    console.log('adViewWillPresentScreen name='+name);
                },
                adViewDidDismissScreen: function(name) {
                    console.log('adViewDidDismissScreen name='+name);
                    plugin.cache(name);
                },
                adViewWillDismissScreen: function(name) {
                    console.log('adViewWillDismissScreen='+name);
                },
                adViewWillLeaveApplication: function(name) {
                    console.log('adViewWillLeaveApplication='+name);
                },
                reward: function(name, currency, amount) {
                
                    console.log('reward='+name+' '+currency+' '+amount);
                    console.log('totalAmount='+self.totalAmount);
                }
            });
            plugin.init();

            if ("undefined" != typeof(plugin.deviceid) && plugin.deviceid.length > 0) {
                console.log(">>>>{0}".format(plugin.deviceid));
                plugin.setTestDevices(plugin.deviceid);
            }

        } else {
            printf("no plugin init")
        }

    }
     
    initSDK();

    sdkbox.PluginAdMob.show("home")

    },


    insufficientCloseClick:function(){

        this.insufficientAllView.active=false
    },

    
    getUserInfoHttp:function(){
        console.log("获取用户信息")
        var that= this
        httpUtils.NhttpPost("/marmot/user_info",{},function(res){

            console.log("用户信息",JSON.stringify(res))  
           
            var stars =parseInt(res["ticket_num"])
            var energy=parseInt(res["energy_num"])
            that.userEnergy=energy
            that.numberCompontent.setText(stars)
            that.energyCompontent.setText(energy)

        }) 

    },

     
    addEnergyClick:function(){
        console.log("增加体力")

        this.exchangeEnergyView.active=true

    },


    chargeEnergyClick:function(){

         var that= this
        httpUtils.NhttpPost("/marmot/star2energy",{},function(res){
            console.log("兑换体力成功",JSON.stringify(res))  
            that.getUserInfoHttp()

            if(cc.isValid(that.showHubProjess)){
                that.showHubProjess.destroy();
              }
              that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
              that.node.addChild(that.showHubProjess);
              that.showHubProjess.getComponent('SVProgressView').init("Increase energy a little")
        
               let finish = cc.callFunc(() => { 
              
                that.showHubProjess.destroy();
            
              }, that);
            let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(0.5), cc.fadeOut(1), finish);
            that.showHubProjess.runAction(action);

           
        },function(failres){
            console.log("兑换体力失败",JSON.stringify(failres))  
            that.insufficientAllView.active=true
        }) 


    },
  
    inviteShare:function(){


    console.log("邀请好友一起玩ss")

    var changebig2 = cc.scaleTo(0.2,1.1);

    var finish = cc.callFunc(function(){

      var changesmail2 = cc.scaleTo(0.1,1.0);
      this.inviteBtn.node.runAction(changesmail2)

      


   }, this);

    var seq = cc.sequence(changebig2,finish)

    this.inviteBtn.node.runAction(seq)

    var that= this

    httpUtils.NhttpPost("/wxmp/marmot/share_msg",{"type":"link","link":"http://www.taoqi001.com/","title":"TapTapMoles","text":"Best Game Engine"},function(res){

     console.log('分享成功',JSON.stringify(res))
     that.shareLink(res);

     

     var _this= that
     httpUtils.NhttpPost("/wxmp/marmot/share",{},function(shareres){

        console.log('分享加体力',JSON.stringify(shareres))
        _this.isshareSuccess=shareres["success"]
        _this.getUserInfoHttp();
    
     })

   },function(failres){
    console.log('分享失败',failres) 
   });


  },


  shareLink: function(sharedic) {
   
    console.log('分享跳转',JSON.stringify(sharedic))
    if (sdkbox.PluginFacebook.isLoggedIn()){
        console.log('分享跳转用户已经登录')
        var info = new Object();
        info.type  = sharedic["type"];
        info.link  = sharedic["link"];
        info.title = sharedic["title"];
        info.text  = sharedic["text"];
        info.image = sharedic["image"];
        sdkbox.PluginFacebook.dialog(info);
    }else{
        console.log('分享跳转用户未登录')
        sdkbox.PluginFacebook.init();
        sdkbox.PluginFacebook.login();
    }
    var  _this=this
    sdkbox.PluginFacebook.setListener({
        onLogin: function(isLogin, msg) {
         
        },
        onGetUserInfo(userInfo){

            var info = new Object();
            info.type  = sharedic["type"];
            info.link  = sharedic["link"];
            info.title = sharedic["title"];
            info.text  = sharedic["text"];
            info.image = sharedic["image"];
            sdkbox.PluginFacebook.dialog(info);

        },

        onSharedSuccess: function(data) {
          
        },
        onSharedFailed: function(data) {
            console.log("share onSharedFailed");
        },
        onSharedCancel: function() {

            console.log("share canceled");

            if(cc.isValid(_this.showHubProjess)){
                _this.showHubProjess.destroy();
              }
          _this.showHubProjess = cc.instantiate(_this.SvprogressPrefab);
          _this.node.addChild(_this.showHubProjess);
          if(_this.isshareSuccess==true){
            _this.showHubProjess.getComponent('SVProgressView').init("Share success and gain 1 energy")
          }else{
            _this.showHubProjess.getComponent('SVProgressView').init("Only share 3 times a day") 
          }
    
           let finish = cc.callFunc(() => { 
          
            _this.showHubProjess.destroy();
        
          },_this);
        let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(3), cc.fadeOut(1), finish);
         _this.showHubProjess.runAction(action);
        },
     
    });

},



  rankView:function(){

    console.log("排行榜");
  
    var changebig2 = cc.scaleTo(0.2,1.1);

    var finish = cc.callFunc(function(){

      var changesmail2 = cc.scaleTo(0.1,1.0);
      this.rankBtn.node.runAction(changesmail2)
      this.wordListView.active=true
      this.wordShowView.getComponent("ListScrollView").init()

   }, this);

    var seq = cc.sequence(changebig2,finish)

    this.rankBtn.node.runAction(seq)

  },

  bageView:function(){

    console.log("兑换包裹");
  },

  moreView:function(){

    console.log("更多");
    
    var changebig2 = cc.scaleTo(0.2,1.1);

    var finish = cc.callFunc(function(){

      var changesmail2 = cc.scaleTo(0.1,1.0);
      this.moreBtn.node.runAction(changesmail2)
      this.moreContentView.getComponent("bageScrollView").init()
      this.bageAllScrollView.active=true

   }, this);

    var seq = cc.sequence(changebig2,finish)

    this.moreBtn.node.runAction(seq)

  },
  
    start () {

    },

});
